﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography;
using System.Text;

namespace Guline.Web.CentralModels
{
    public class RSACryto
    {
        private string sPrivateKey;
        private string sPublickey;
        private RSAParameters pubKey;
        private RSAParameters priKey;

        public RSACryto()
        {

        }
        public void GenerateKeys()
        {
            RSACryptoServiceProvider csp = new RSACryptoServiceProvider(2048);
            priKey = csp.ExportParameters(true);
            pubKey = csp.ExportParameters(false);
        }
        public string getSpublicKey()
        {
            var sw = new System.IO.StringWriter();
            //we need a serializer
            var xs = new System.Xml.Serialization.XmlSerializer(typeof(RSAParameters));
            //serialize the key into the stream
            xs.Serialize(sw, pubKey);
            //get the string from the stream
            return sw.ToString();
        }
        public string getsPrivateKey()
        {
            var sw = new System.IO.StringWriter();
            //we need a serializer
            var xs = new System.Xml.Serialization.XmlSerializer(typeof(RSAParameters));
            //serialize the key into the stream
            xs.Serialize(sw, priKey);
            //get the string from the stream
            return sw.ToString();
        }
        public void pubKeyFromString(string skey)
        {
            var sr = new System.IO.StringReader(skey);
            //we need a deserializer
            var xs = new System.Xml.Serialization.XmlSerializer(typeof(RSAParameters));
            //get the object back from the stream
            pubKey = (RSAParameters)xs.Deserialize(sr);
        }
        public void priKeyFromString(string skey)
        {
            var sr = new System.IO.StringReader(skey);
            //we need a deserializer
            var xs = new System.Xml.Serialization.XmlSerializer(typeof(RSAParameters));
            //get the object back from the stream
            priKey = (RSAParameters)xs.Deserialize(sr);
        }

        public string Encrypt(string data)
        {
            RSACryptoServiceProvider csp = new RSACryptoServiceProvider();
            csp.ImportParameters(pubKey);

            var bytesPlainTextData = System.Text.Encoding.Unicode.GetBytes(data);

            //apply pkcs#1.5 padding and encrypt our data 
            var bytesCypherText = csp.Encrypt(bytesPlainTextData, false);

            //we might want a string representation of our cypher text... base64 will do
            return Convert.ToBase64String(bytesCypherText);
        }
        public string Decrypt(string cypher)
        {
            var bytesCypherText = Convert.FromBase64String(cypher);

            //we want to decrypt, therefore we need a csp and load our private key
            RSACryptoServiceProvider csp = new RSACryptoServiceProvider();
            csp.ImportParameters(priKey);

            //decrypt and strip pkcs#1.5 padding
            var bytesPlainTextData = csp.Decrypt(bytesCypherText, false);

            //get our original plainText back...
            return System.Text.Encoding.Unicode.GetString(bytesPlainTextData);
        } 
    }
}
